//
//  PopoverAnimator.swift
//  swift-微博
//
//  Created by chenrin on 2016/12/15.
//  Copyright © 2016年 zhuoheng. All rights reserved.
//

import UIKit

let popoverAnmiatorShow = "popoverAnmiatorShow"
let popoverAnmiatorDismiss = "popoverAnmiatorDismiss"

class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    var presentFrame = CGRect.zero

    //记录是展开还是消失
    var isPresent : Bool = true
    
    //实现代理方法，告诉系统谁来负责转场动画
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let pc = PresentationController(presentedViewController: presented, presenting: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    
    
    ///只要实现以下方法，系统默认动画就没有了，所有的需自己来实现
    
    //告诉系统谁来负责展现动画，presented:被展现的视图，presenting:发起的视图
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = true
        
        //发送通知控制器即将展开
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: popoverAnmiatorShow), object: nil)
        return self
    }
    //消失的视图
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = false
         NotificationCenter.default.post(name: NSNotification.Name(rawValue: popoverAnmiatorDismiss), object: nil)
        return self
    }
    
    
    // MARK:- UIViewControllerAnimatedTransitioning
    //返回动画时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
        return 0.5
    }
    //告诉系统如何动画
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
        //1.拿到展现的视图
        //        let toVc = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        //        let fromVc = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        if isPresent {
            let toView = transitionContext.view(forKey: .to)
            toView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
            //将视图添加到容器上
            transitionContext.containerView.addSubview(toView!)
            //设置锚点
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            
            UIView.animate(withDuration: 0.5, animations: {
                //清空transform
                toView?.transform = CGAffineTransform.identity
            }) { (_) in
                //这句话必须写
                transitionContext.completeTransition(true)
            }
            
        }else{
            let fromView = transitionContext.view(forKey: .from)
            
            
            UIView.animate(withDuration: 0.2, animations: {
                fromView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.000001)
            }) { (_) in
                //这句话必须写
                transitionContext.completeTransition(true)
            }
        }
        
        
    }


}
